<?php
	class Trivia_model extends MY_Model{
		
		public function __construct(){
			$this->load->database();
		}
		
		public function get_trivia($id=FALSE){
			if($id === FALSE){
				$this->db->select('*');
				$this->db->from('trivia_game');
				$this->db->limit($this->record_per_page, $this->limit);
				$query = $this->db->get();
				return $query->result_array();
			}
			$this->db->limit($this->record_per_page, $this->limit);
			$query = $this->db->get_where('trivia_game', array('trivia_game.id' => $id));
			return $query->row_array();
		}
		
		public function get_trivia_list(){
			$query = $this->db->get('trivia_game');
			return $query->result_array();
		}
		
		public function get_total(){
			return count($this->get_trivia_list());
		}
		
		public function get_total_search($src=FALSE){
			$this->db->select('*');
			$this->db->from('trivia_game');
			$this->db->like('title', $src);
			$this->db->or_like('description', $src);				
			$query = $this->db->get();				
			return $query->num_rows();
		}
		
		public function search($src=FALSE){
			$this->db->select('*');
			$this->db->from('trivia_game');
			$this->db->like('title', $src);
			$this->db->or_like('description', $src);		
			$this->db->limit($this->record_per_page, $this->limit);
			$query = $this->db->get();				
			return $query->result_array();
		}
		
		public function save(){
			$id 	= $this->input->post('id');
			
			$data = array(
				"title" 		=> $this->input->post('title'),
				"description" 	=> $this->input->post('description'),				
			);
			if(empty($id))
				return $this->db->insert('trivia_game', $data);
			else{
				$this->db->where('id', $id);
				return $this->db->update('trivia_game', $data);
			}
		}
		
		public function delete($id){
			$sql = "SELECT `id` FROM `question` WHERE `trivia_game_id` = $id";
			$query = $this->db->query($sql);
			if($query->num_rows()==0){
				$this->db->where('id', $id);
				$this->db->delete('trivia_game');
			}
		}
		
		public function save_assign(){
			// set all trivia game not assigned
			$sql = "UPDATE `trivia_game` SET `assigned`=0 WHERE 1=1";
			$query = $this->db->query($sql);
			
			// set submited trivia game assigned
			$id = $this->input->post('id');
			$sql = "UPDATE `trivia_game` SET `assigned`=1 WHERE `id`=$id";
			$query = $this->db->query($sql);
			
			// set all question not assigned
			$sql = "UPDATE `question` SET `assigned`=0 WHERE 1=1";
			$query = $this->db->query($sql);
			
			// set submited question assigned
			$q_id = $this->input->post('q_id');
			$arr = implode(",",$q_id);
			$sql = "UPDATE `question` SET `assigned`=1 WHERE `id` IN($arr)";
			$query = $this->db->query($sql);
		}
		
		public function trivia_game_assign(){
			$this->db->select('t.title, q.question, q.id');
			$this->db->from('trivia_game t');
			$this->db->join('question q', 'q.trivia_game_id=t.id', 'left');			
			$this->db->where('t.assigned =1', NULL, FALSE);
			$this->db->where('q.assigned =1', NULL, FALSE);
			$this->db->group_by('q.id');
			$query = $this->db->get();
			$arr = $query->result_array();
			$xml = '<?xml version="1.0" encoding="utf-8"?>';
			$xml.='<trivia_game>';
			
			if(count($arr)){
				foreach($arr as &$v){
					$this->db->select('answer, mark, id');
					$this->db->from('answer');
					$this->db->where('question_id = '.$v['id'], NULL, FALSE);
					$query 			= $this->db->get();
					$suggest 		= $query->result_array();
					$v['suggest'] 	= $suggest;
					$mark			= 0;
					$aws				= 0;
					$aws_text		= '';
					
					$xml.='<question><title>'.$v['question'].'</title>';
					$xml.='<suggestion>';
					
					foreach($suggest as $item){
						if($mark < $item['mark']){
							$mark 		= $item['mark'];
							$aws 			= $item['id'];
							$aws_text	= $item['answer'];
						}
						$xml.='<item id="' . $item['id'] . '">' . $item['answer'] . '</item>';							
					}
					$xml.='</suggestion>';
					$v['asw'] = $aws;
					$xml.='<answer><item id="' . $aws . '">' . $aws_text . '</item></answer>';		
					$xml.='</question>';
				}
				$xml.='</trivia_game>';
				return $xml;				
			}
			else return FALSE;
		}
				
	}
?>